{% stylesheet %}
.block_product_single {
  padding: var(--general_layout_spacing) 0;
  display: flex;
}

@media screen and (max-width: 767px) {
  .block_product_single {
    padding: calc(var(--general_layout_spacing) * 0.5) 0;
  }
}

.block_product_single .product-photo-item {
  display: flex;
}

.block_product_single .product-photo-item a {
  display: block;
  width: 100%;
}

.block_product_single .product-photo-item img {
  width: 100%;
  height: auto;
  vertical-align: top;
}

.block_product_single .product-photo {
  flex: 1;
  flex-shrink: 0;
  overflow: hidden;
}

.block_product_single .product-info {
  flex: 1;
  flex-shrink: 0;
  box-sizing: border-box;
  padding-left: 30px;
  display: flex;
  flex-direction: column;
}

.block_product_single .product-info .product-price {
  display: flex;
  font-size: 20px;
  justify-content: center;
  padding-top: 20px;
}

.block_product_single .product-info .product-info-title {
  text-align: center;
}

.block_product_single .product-info .product-info-describe {
  padding-top: 20px;
  color: var(--detail_color);
  font-size: 14px;
  text-align: center;
}

.block_product_single .product-info .product-info-subtitle {
  padding-top: 20px;
  color: var(--title_color);
  font-size: 14px;
  text-align: center;
}

.block_product_single .product-info .product-info-divider {
  width: 50px;
  height: 1px;
  background-color: #1d1f21;
  margin: 30px auto 0 auto;
}

.block_product_single .product-info .product-info-star {
  padding-top: 20px;
  display: flex;
  justify-content: center;
}

.block_product_single .cell-item {
  margin-top: 30px;
  display: flex;
  justify-content: center;
}

@media screen and (max-width: 767px) {
  .block_product_single .cell-item {
    margin-top: 20px;
  }
}

.block_product_single .cell-item .product-sku-name {
  font-size: 14px;
  color: #666666;
}

.block_product_single .product-info-quantity {
  height: 44px;
  box-sizing: border-box;
  display: flex;
  justify-content: center;
}

.block_product_single .product-info-quantity .product-info-quantity-handle, .block_product_single .product-info-quantity .product-calculator-value {
  width: 44px;
  text-align: center;
  line-height: 44px;
  box-sizing: border-box;
  position: relative;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}

.block_product_single .product-info-quantity .product-calculator-value {
  margin: 0 30px;
  font-size: 1rem;
  color: #1d1f21;
}

.block_product_single .product-info-btns {
  display: flex;
  width: 100%;
}

@media screen and (max-width: 767px) {
  .block_product_single .product-info-btns {
    flex-direction: column;
  }
}

.block_product_single .product-info-btns .product-info-btn {
  flex: 1;
}

.block_product_single .product-info-btns .secondary_btn {
  margin-right: 15px;
}

@media screen and (max-width: 767px) {
  .block_product_single .product-info-btns .secondary_btn {
    margin-right: 0;
  }
}

.block_product_single .product-info-btns .main_btn {
  margin-left: 15px;
}

@media screen and (max-width: 767px) {
  .block_product_single .product-info-btns .main_btn {
    margin-top: 20px;
    margin-left: 0;
  }
}

@media screen and (max-width: 767px) {
  .block_product_single {
    flex-direction: column;
  }
  .block_product_single .product-info {
    padding-left: 0;
    margin-top: 20px;
  }
}

.block_product_single .product-sku-values {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-wrap: wrap;
}

.block_product_single .product-sku-values .product-sku-select {
  box-sizing: border-box;
  border: 1px solid #d4d4d4;
  cursor: pointer;
  position: relative;
}

.block_product_single .product-sku-values .product-sku-select .mo-edge {
  position: absolute;
  right: 10px;
  top: 50%;
  margin-top: -3px;
  cursor: pointer;
  border-style: dashed;
  border-color: transparent;
  border-width: 4px;
  border-top-color: var(--color-main);
  border-top-style: solid;
  transition: all 0.3s;
  -webkit-transition: all 0.3s;
}

.block_product_single .product-sku-values .product-sku-unselect {
  color: #1d1f21;
  font-size: 14px;
  line-height: 18px;
  padding: 6px 34px 6px 2px;
  position: relative;
  outline: none;
  z-index: 5;
  border: none;
  cursor: pointer;
  text-align: center;
  background-color: transparent;
  appearance: none;
  -moz-appearance: none;
  -webkit-appearance: none;
  border: 0;
}

.block_product_single .product-sku-values .product-sku-values-item {
  color: #1d1f21;
  font-size: 14px;
  line-height: 18px;
  padding: 8px 15px;
  box-sizing: border-box;
  border: 1px solid transparent;
  cursor: pointer;
  text-align: center;
  margin-bottom: 12px;
  display: flex;
  align-items: center;
}

.block_product_single .product-sku-values .product-sku-values-item img {
  width: 40px;
  vertical-align: middle;
  height: 40px;
  margin-right: 6px;
  object-fit: cover;
}

.block_product_single .product-sku-values .product-sku-values-item + .product-sku-values-item {
  margin-left: 20px;
}

.block_product_single .product-sku-values .product-sku-values-item:hover {
  border: 1px solid #1D1F21;
}

.block_product_single .product-sku-values .product-sku-values-item-active {
  border: 1px solid #1D1F21;
}

.block_product_single .comment-fraction {
  display: flex;
  align-items: center;
}

.block_product_single .comment-fraction .comment-star-warp {
  position: relative;
  display: flex;
  align-items: center;
}

.block_product_single .comment-fraction .comment-star-warp-choose {
  position: absolute;
  top: 0;
  left: 0;
  bottom: 0;
  display: flex;
  align-items: center;
  overflow: hidden;
}

.block_product_single .comment-fraction .comment-star-warp-choose svg {
  flex-shrink: 0;
}

.block_product_single .comment-fraction svg {
  margin-right: 4px;
}

.block_product_single .comment-fraction svg:last-child {
  margin-right: 0;
}

.block_product_single .comment-fraction span {
  font-weight: 500;
  color: #1d1f21;
  font-size: 18px;
  line-height: 1;
  margin-left: 6px;
}

.block_product_single-right {
  display: flex;
  flex-direction: row-reverse;
}

@media screen and (max-width: 767px) {
  .block_product_single-right {
    flex-direction: column;
  }
}

.block_product_single-right .product-info {
  padding: 0;
}

.block_product_single-right .product-photo {
  margin-left: 30px;
}

@media screen and (max-width: 767px) {
  .block_product_single-right .product-photo {
    margin-left: 0;
    margin-top: 0;
  }
}

{% endstylesheet %}
{% assign defaultImageEmpty = 'empty.png' | public_asset_abs_url   %}
{% assign block_id = block_id | default : section.block_id  %}


{% get_product product_id={section.settings.good.id} %}

{% capture showProduct %}
{%- if section.settings.good.id != "" -%}
{%- unless product -%}
false
{%- else -%}
true 
{%- endunless -%}
{%- else -%}
true    
{%- endif -%}

{% endcapture %}


{% if showProduct == 'true' %}

{%- capture handle -%}
{%- if section.settings.good.id != "" -%}
/products/{{ product.handle }}?data_from={{data_from}}
{%- else -%}
javascript:;
{%- endif -%}
{%- endcapture -%}

<div class="container-wrapper">
<div id="block_product_single-{{ block_id }}" class="block_product_single block_product_single-{{ section.settings.position }} ">
  <div class="product-photo">
      <div class="product-photo-item">
          <a href="{{handle}}">
            {% if section.settings.good.id == "" %}
            <img src="{{ 'empty.png' | public_asset_abs_url }}"  />
            {% else %}
            <img data-src="{{ product.image.src }}" class="product-photo-image" src="{{ 'empty.png' | public_asset_abs_url }}"   alt="{{  product.image.alt | default: product.title }}" />
            {% endif %}
          </a> 
      </div>
  </div>
  <div class="product-info" id="sku-option-{{ block_id }}">
      <!-- title -->
      <a class="product-info-title general_title-color" href="{{handle}}">{{ product.title | default: "Title" }}</a>
      {% if product.comment_count > 0 %}
      <div class="product-info-star">
      <div class="comment-fraction">
        <div class="comment-star-warp">
            {% for index in (1..5) %}
            {% include "icon_block_star" ,width:"20",height:"20",color: "#bbbbbb" %}
            {% endfor %}
            {% assign star = 0 %}
            {% if  product.star %}  {% assign star = product.star | divided_by: 5 | times:100 %} {% endif %}
            <div class="comment-star-warp-choose" style="width:{{star}}%">
              {% for index in (1..5) %}
              {% include "icon_block_star" ,width:"20",height:"20",color: "#F8AD6B" %}
              {% endfor %}
            </div>
        </div>
        <span>({{product.star}})</span>
      </div>
      </div>
      {% endif %}

      {% if section.settings.subtitle and product.subtitle != "" %}
      <div class="product-info-subtitle">{{ product.subtitle | default : "subtitle" }}</div>
      {% endif %}

      {% if section.settings.describe and product.mini_detail != "" %}
      <div class="product-info-describe">{{ product.mini_detail | default : "mini_detail" }}</div>
      {% endif %}
      
      <div class="product-price">
              <span class="product-price-number general-price">{{ currency.symbol_left }}{{ product.variant_price_max | default: "0.00" }}</span>
              {% if product.variant_compare_at_price_max %}
              <span class="product-price-market general-oldprice">{{ currency.symbol_left }}{{ product.variant_compare_at_price_max }}</span>
              {% endif %}
      </div>

      <div class="product-info-divider"></div>

      <div class="sku-cell"></div>
      <div class="cell-item">
          <span class="product-sku-name">{{lang.cart.list.quantity}}</span>
      </div>
      <div class="cell-item">
          <div class="product-info-quantity mo-quantity" data-id="{{ block_id  }}">
              <div class="product-info-quantity-handle product-calculator-handle" data-type="reduce">
                  {% include "icon_minus" ,width:'16',height:'16' %}
              </div>
              <div class="product-calculator-value">1</div>
              <div class="product-info-quantity-handle product-calculator-handle" data-type="add">
                  {% include "icon_plus" ,width:'16',height:'16' %}
              </div>
          </div>
      </div>
      <div class="cell-item">
          <div class="product-info-btns">
              {% if product.available == 0  %}
              <div class="disable-btn product-info-btn">{{lang.product.out_of_stock}}</div>
              {% else %}
              <div class="secondary_btn product-info-btn add-cart" data-type="cart" data-id="{{ block_id  }}">{{lang.product.add_to_cart}}</div>
              <div class="main_btn product-info-btn buynow" data-type="buyNow" data-id="{{ block_id  }}">{{lang.product.buy_it_now}}</div>
              {% endif %}

          </div>
      </div>
      {%- if section.settings.good.id != "" and product.available != 0  -%}
      {% get_payments %}
      {% if payment %}
          <div class="cell-item" style="margin-top:17px">
              {% include "product_payment_paypalcheckout" , pay_block_id:block_id  %}
          </div>
      {% endif %}
      {% endif %}
  </div>
</div>
</div>
{%- if section.settings.good.id != "" -%}
   <script type="text/javascript">
    (function(){
        var productInfo = {{product | json }};

        const productDOM = $('#block_product_single-{{ block_id }}');

        const productDetail = new omesaasProduct({
            product: productInfo,
            data_from:'{{ data_from }}',
            ec_currency_code: '{{ base_currency.standard_code }}',
            id: '#block_product_single-{{ block_id }}',
            changeOption: function (params) {
                if(params.image && params.image.src){
                    const imgNode = productDOM.find(".product-photo-image");
                    imgNode.attr('data-src',params.image.src);
                    addLazyImages(imgNode)
                }
                //设置页面缓存
                moi.setPageStorage('product_params', params);
            }
        })


        //payPal支付成功回调
        moi.addEvent('paypalec_callback', function (event) {
            //判断支付成功payid是否等于当前积木块id
            if(event.params.payId === '{{block_id}}'){
                productDetail.callbackAddToCart();
            }
        })
        
        console.log(productInfo)

        moi.quantity({
            dom:  productDOM.find(".mo-quantity"),
            change(val) {
                productDetail.setSelectedVal({ quantity: val.value });
                qty = val.value;
            }
        })


        //加入购物车
        $(".add-cart[data-id='{{ block_id | default: 'test' }}']").click(function () {
            if (!productDetail.verifyOption('cart')) {
                return false
            }
            var load = moi.nodeShowLoading(this, "mo-loading-black")
            productDetail.addCart(function () { load.close(); }, function (data) {
                if (!data.code) {
                    productDetail.cartLayer();
                } else {
                    return moi.alert({
                        title: data.msg
                    });
                }
            })
        })


        //直接购买
        $(".buynow[data-id='{{ block_id | default: 'test' }}']").click(function () {
            if (!productDetail.verifyOption('buy')) {
                return false
            }
            var load = moi.nodeShowLoading(this)
            productDetail.buynow(function () { load.close(); }, function (ret) {
                if (!ret.code) {
                    window.location.href = ret.data.checkout_url
                } else {
                    return moi.alert({
                        title: ret.msg
                    });
                }
            })
        })

    })()
</script> 
{% endif %}
{%- endif -%}

{% schema %}
{
	"tag": "",
	"class": "block_product_single",
	"icon": "icon-dangeshangpin",
	"is_global": false,
	"name": {
		"zh_CN": "单个商品"
	},
	"max_blocks": "",
	"settings": [
		{
			"type": "card_header",
			"label": {
				"zh_CN": "设置"
			}
		},
		{
			"type": "card_product",
			"label": {
				"zh_CN": "选择商品"
			},
			"default": {
				"id": "id",
				"title": ""
			},
			"id": "good"
		},
		{
			"type": "card_switch",
			"label": {
				"zh_CN": "显示副标题"
			},
			"id": "subtitle",
			"default": true
		},
		{
			"type": "card_switch",
			"label": {
				"zh_CN": "显示简短描述"
			},
			"id": "describe",
			"default": true
		},
		{
			"type": "card_select",
			"label": {
				"zh_CN": "pc图片位置"
			},
			"default": "left",
			"id": "position",
			"option": [
				{
					"label": {
						"zh_CN": "居左"
					},
					"value": "left"
				},
				{
					"label": {
						"zh_CN": "居右"
					},
					"value": "right"
				}
			]
		}
	],
	"blocks": [],
	"default": {
		"settings": {
			"good": {
				"id": "",
				"title": ""
			},
			"subtitle": true,
			"describe": true,
			"position": "right"
		},
		"blocks": []
	}
}
{% endschema %}